using System;
using System.Collections.Generic;
using System.Text;
using RapidHDL;

namespace RHDLSynthOnly
{
    class Program
    {
        static void Main(string[] args)
        {
			bool bDebug = false;
			
			while(true)
			{
				if (WriteHandshake("ready"))
				{
					System.Console.WriteLine("RHDLSynthOnly Ready...");
				}
				else
				{
					System.Console.WriteLine("Error writing to file.");
					return;
				}
				
				string sHandshake = "";
				while (sHandshake != "go" && !bDebug)
				{
					WriteHandshake("ready");
					System.Threading.Thread.Sleep(2000);
					System.Console.Write(".");
					sHandshake = ReadHandshake();
				}
				
				WriteHandshake("working");
                
                System.Console.WriteLine("\n\nUnpacking...");
                XilinxTools.UnpackUploadedDirectory("/home/calibre/rhdl_out/","v");


				System.Console.WriteLine("\n\nCompiling...");
				
				
				RapidHDL.Settings oSettings = new Settings();
	            string sBuildErrors = 
	                XilinxTools.LaunchXilinxBuild(
	                    "", 
	                    "/home/calibre/rhdl_out/", 
	                    oSettings.TopComponentName, 
	                    oSettings.XilinxChip, 
	                    oSettings.OptimizeSpeed,
	                    "CFBase-noppc.ucf"
	                    );
	            if (sBuildErrors == "")
				{
					WriteHandshake("success");
	                Console.WriteLine("\n OK!");
				}
	            else
				{
					WriteHandshake(sBuildErrors);
	                Console.WriteLine(sBuildErrors);
				}

				if (bDebug)
					return;
				
				Console.WriteLine("Waiting For Client");
				sHandshake = "";
				while (sHandshake != "done")
				{
					System.Threading.Thread.Sleep(2000);
					System.Console.Write(".");
					sHandshake = ReadHandshake();
				}

			}
        }

		
		public static string ReadHandshake()
        {
            return ReadStatusFile("handshake.txt");
        }

        public static bool WriteHandshake(string psStatus)
        {
            WriteStatusFile("handshake.txt", psStatus);
            return true;
        }

        public static string WriteStatusFile(string psFileName, string psStatus)
        {
            try
            {
                System.IO.StreamWriter oFile = RapidHDL.TextFile.OpenFileReWrite(psFileName);
                oFile.Write(psStatus);
                oFile.Close();
                return "";
            }
            catch
            {
                return "Unable to open status file : " + psFileName.ToString();
            }
        }

        public static string ReadStatusFile(string psFileName)
        {
            try
            {
                System.IO.StreamReader oFile = RapidHDL.TextFile.OpenFileRead(psFileName);
                string sResult = oFile.ReadToEnd();
                oFile.Close();
                return sResult;
            }
            catch
            {
                return "Unable to read status file : " + psFileName.ToString();
            }
        }

	}
}
